using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SpoonCore.Message.Info;

namespace SpoonCore.Message
{
	public class LookupMessage : IMessage
	{
		private int _id;
		private SpoonIdentifier _lookupId;
		private ClientInfo _sourceClient;
		private LookupType _type = LookupType.Request;
		private int _hopCount = 0;

		public enum LookupType : byte { Request, Response }

		public int MsgId
		{
			get { return 3; }
		}

		public SpoonIdentifier LookupId
		{
			get { return _lookupId; }
			set { _lookupId = value; }
		}

		public LookupType Type
		{
			get { return _type; }
			set { _type = value; }
		}

		public ClientInfo SourceClient
		{
			get { return _sourceClient; }
			set { _sourceClient = value; }
		}

		public int HopCount
		{
			get { return _hopCount; }
		}

		public int Id
		{
			get { return _id; }
			set { _id = value; }
		}

		public byte[] GetBytes()
		{
			MemoryStream stream = new MemoryStream();
			BinaryWriter bw = new BinaryWriter(stream);

			bw.Write(_id);
			bw.Write(LookupId.GetBytes());
			bw.Write((byte)Type);
			bw.Write(SourceClient.GetBytes());
			bw.Write(_hopCount);

			return stream.ToArray();
		}

		public void SetBytes(SpoonClient client, BinaryReader br)
		{
			_id = br.ReadInt32();
			LookupId = new SpoonIdentifier(br);
			Type = (LookupType) br.ReadByte();
			SourceClient = new ClientInfo(client, br);
			_hopCount = br.ReadInt32();
		}

		public void AddHop()
		{
			_hopCount++;
		}

		public IMessage Clone()
		{
			return new LookupMessage();
		}

		public MessageInfo Info
		{
			get { return null; }
			set { }
		}
	}
}
